﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Domain
{
    [TestClass]
    public class SearchContentFilterTests
    {
        [TestMethod]
        public void Constructor_EmptyString_DefaultValues()
        {
            SearchContentFilter filter = new SearchContentFilter("");
            Assert.AreEqual(-1, filter.CategoryId);
            Assert.AreEqual(10, filter.PageSize);
            Assert.AreEqual(0, filter.CurrentPage);
            Assert.AreEqual(SortDirection.Ascending, filter.SortDirection);
        }
        
        [TestMethod]
        public void Constructor_SomeInput_MatchingValues()
        {
            SearchContentFilter filter = new SearchContentFilter("CategoryID=666&CurrentPage=2&PageSize=5");
            Assert.AreEqual(666, filter.CategoryId);
            Assert.AreEqual(5, filter.PageSize);
        }
        
        [TestMethod]
        public void GetQuery_FilterIsSet_MatchingQuery()
        {
            SearchContentFilter filter = new SearchContentFilter("");
            filter.CategoryId = 3;
            filter.PageSize = 5; // Important! PageSize must be set before CurrentPage or the latter is updated automatically
            filter.CurrentPage = 2;
            Assert.AreEqual("CategoryID=3&LocaleID=0&CurrentPage=2&PageSize=5&Freetext=&ContentName=&SortMode=SortKey&SortDirection=Ascending", filter.GetQuery());
        }
        
        [TestMethod]
        public void SetContentCategory_IsAlreadySet_CurrentPageIsBeingResetToZero()
        {
            SearchContentFilter filter = new SearchContentFilter("");
            filter.CategoryId = 3;
            filter.CurrentPage = 2;
            Assert.AreEqual(2, filter.CurrentPage);
            filter.CategoryId = 5;
            Assert.AreEqual(0, filter.CurrentPage);
        }
        
        [TestMethod]
        public void SetPageSize_IsAlreadySet_CurrentPageIsBeingRecalculated()
        {
            SearchContentFilter filter = new SearchContentFilter("");
            filter.PageSize = 10;
            filter.CurrentPage = 2;
            Assert.AreEqual(2, filter.CurrentPage);
            filter.PageSize = 5;
            Assert.AreEqual(4, filter.CurrentPage);
            filter.PageSize = 20;
            Assert.AreEqual(1, filter.CurrentPage);
        }
    }
}
